//
//  CheckAndAcceptViewController.m
//  MyProject
//
//  Created by Pei on 16/6/29.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "CheckAndAcceptViewController.h"
#import "IHRefreshTableView.h"
#import "CheckAndAcceptCell.h"
#import "CheckDetailViewController.h"
#import "MultiSelectTableViewCell.h"
#import "NSMutableArray+Additions.h"
#import "ShiGongStatusViewController.h"
#import "SelectCompanyAndProjectViewController.h"
#import "UserInfo.h"
#import "GongXuDetailViewController.h"


@interface CheckAndAcceptViewController ()<UITableViewDelegate, UITableViewDataSource,ShiGongStatusViewControllerDelegate, SelectCompanyAndProjectViewControllerDelegate>
{
    IHRefreshTableView *_myTable;
    UIView *_headerView;
    
    UIButton *_rightBtn;
    
    UIControl *_addControl;
    UIView *_addView;
    
    NSString *_currentAction;
    
    NSDictionary *firstDic;
    NSDictionary *secondDic;
    
    int status;
    
    UILabel *lbl0;
    UILabel *lbl1;
    UILabel *lbl2;
    
    NSArray *_dataArray;
}

@end

@implementation CheckAndAcceptViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Hide_TabBar_Notification object:nil];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:Dic_ProjectState];
    if (tempArray != nil && tempArray.count > 0) {
    }else{
        [self getProjectStateData];
    }
    
    status = 0;
    
    _rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    //[rightBtn setBackgroundImage:[UIImage imageNamed:@"02edit"] forState:UIControlStateNormal];
    //[rightBtn setBackgroundImage:[UIImage imageNamed:@"02edit"] forState:UIControlStateHighlighted];
    [_rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [_rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    _rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [_rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    //[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];

    
    _selFlags = [[NSMutableArray alloc] initWithCapacity:3];
    for (int i = 0; i < 3; i++)
    {
        [_selFlags addObject:[NSNumber numberWithBool:NO]];
    }

    
    
    [self initTableView];
    [self initTableHeaderView];
    
    _addControl = [[UIControl alloc] init];
    _addControl.frame = CGRectMake(0.0f, kDeviceHeight-NAV_HEIGHT-kTabHeight, kDeviceWidth, kTabHeight);
    [_addControl addTarget:self action:@selector(addControlAction) forControlEvents:UIControlEventTouchUpInside];
    _addControl.backgroundColor = kBtnBackgroundColor;
    //[self.view addSubview:_addControl];
    
    //02edit@3x
    UILabel *btnTitle = [[UILabel alloc] initWithFrame:CGRectMake((kDeviceWidth-66)/2.0f, (_addControl.height-Home_Big_Size)/2.0f, 66, Home_Big_Size)];
    btnTitle.textColor = kWhiteColor;
    btnTitle.text = @"添加任务";
    btnTitle.font = kAppFont(Home_Big_Size);
    [_addControl addSubview:btnTitle];
    
    _addView = [[UIView alloc] init];
    _addView.hidden = YES;
    _addView.frame = CGRectMake(0.0f, kDeviceHeight-NAV_HEIGHT-kTabHeight, kDeviceWidth, kTabHeight);
    [self.view addSubview:_addView];
    
    //114 122 162
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleBtn.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth/3.0f, kTabHeight);
    [cancleBtn setBackgroundColor:kRGB(114, 122, 162)];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = kAppFont(Home_Big_Size);
    cancleBtn.tag = 116;
    [cancleBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_addView addSubview:cancleBtn];
    
    UIButton *uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    uploadBtn.frame = CGRectMake(kDeviceWidth/3.0f, 0.0f, kDeviceWidth/3.0f, kTabHeight);
    [uploadBtn setBackgroundColor:kBtnBackgroundColor];
    [uploadBtn setTitle:@"提交" forState:UIControlStateNormal];
    [uploadBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    uploadBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [uploadBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    uploadBtn.tag = 118;
    [_addView addSubview:uploadBtn];
    
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(kDeviceWidth/3.0f*2.0f, 0.0f, kDeviceWidth/3.0f, kTabHeight);
    [deleteBtn setBackgroundColor:kRedColor];
    [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [deleteBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    deleteBtn.titleLabel.font = kAppFont(Home_Big_Size);
    deleteBtn.tag = 119;
    [deleteBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_addView addSubview:deleteBtn];
    
    [self getCompanyListData];
    

}

- (void)getProjectStateData{
    [[YMNetLib getInstance] connectWithAction:Dictionary_Type_API method:@"GET" withContent:@"DicType=ProjectState" withBody:nil withDelegate:self];
    _currentAction = @"Dic_ProjectState";
}

- (void)getCompanyListData{
    //Get_Company_List
    [[YMNetLib getInstance] connectWithAction:Get_Company_List method:@"GET" withContent:[NSString stringWithFormat:@"UserID=%@",[[UserInfo getInstance] getUserID]] withBody:nil withDelegate:self];
    _currentAction = Get_Company_List;
}

- (void)getProjectListData:(NSString*)OrgCode{
    //Get_Project_List
    [[YMNetLib getInstance] connectWithAction:Get_Project_List method:@"GET" withContent:[NSString stringWithFormat:@"orgCode=%@", OrgCode] withBody:nil withDelegate:self];
    _currentAction = Get_Project_List;
}

- (void)getData{
    //Get_Important_Working_List
    [[YMNetLib getInstance] connectWithAction:Get_Important_Working_List method:@"GET" withContent:[NSString stringWithFormat:@"UserID=%@&projectId=%d&Keyword=%@&SGStatus=%d&Status=%@&pagesize=%@&pageindex=%@",[[UserInfo getInstance] getUserID], [[secondDic valueForKey:@"ProjectID"] intValue], @"", status, @"", @"10", @"1"] withBody:nil withDelegate:self];
    _currentAction = Get_Important_Working_List;
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([_currentAction isEqualToString:Get_Important_Working_List]){
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            _dataArray = [body objectForKey:@"Items"];
            
            [_myTable reloadData];
        }
        //Items
        
    }else if ([_currentAction isEqualToString:Get_Company_List]) {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            NSArray *firstDataArray = [body valueForKey:@"OrgList"];
            
            NSMutableArray *dddd = [[NSMutableArray alloc] initWithCapacity:3];
            [dddd removeAllObjects];
            for (NSDictionary *ttt in firstDataArray) {
                if ([[ttt valueForKey:@"OrgLevel"] intValue] == 0) {
                    [dddd addObject:ttt];
                }
            }
            if (dddd != nil && dddd.count >0) {
                NSDictionary *tempDic = [dddd objectAtIndex:0];
                firstDic = tempDic;
                
                [self getProjectListData:[tempDic objectForKey:@"OrgCode"]];
            }
            
        }
    }else if ([_currentAction isEqualToString:Get_Important_Working_List]){
        
    }else if ([_currentAction isEqualToString:@"Dic_ProjectState"])
    {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
        {
            [[NSUserDefaults standardUserDefaults] setObject:[body valueForKey:@"DicList"] forKey:Dic_ProjectState];
            [_myTable reloadData];
        }
    }else{
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            NSArray *secondDataArray = [body valueForKey:@"ProjectList"];
            NSDictionary *tempDic = [secondDataArray objectAtIndex:0];
            secondDic = tempDic;
            
            lbl0.text = [firstDic valueForKey:@"OrgName"];
            lbl1.text = [secondDic valueForKey:@"ProjectName"];
            
            [self getData];
        }
    }
}


- (void)rightBtnAcion
{
    [_myTable setEditing:!_myTable.editing animated:YES];
    
    _addControl.hidden = YES;
    _addView.hidden = NO;
    
    [self.navigationItem setRightBarButtonItem:nil];
}

- (void)addControlAction
{
    
}

- (void)btnAction:(UIButton*)sender
{
    switch (sender.tag) {
        case 116:
        {
            [_myTable setEditing:!_myTable.editing animated:YES];
            
            _addView.hidden = YES;
            _addControl.hidden = NO;
            
            [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:_rightBtn]];
        }
            break;
            
        default:
            break;
    }
}

- (void)initTableView
{
    CGRect frame = CGRectMake(0, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT);
    _myTable = [[IHRefreshTableView alloc] initWithFrame:frame refreshType:-1 andAutoLoad:NO];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    _myTable.footer.hidden = YES;
    _myTable.header.hidden = YES;
    _myTable.allowsSelectionDuringEditing = YES;
    [self.view addSubview:_myTable];
}

- (void)initTableHeaderView
{
    _headerView = [[UIView alloc] init];
    _headerView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, 40.0f);
    _headerView.backgroundColor = kWhiteColor;
    _myTable.tableHeaderView = _headerView;
    
    UIView *lineView0 = [[UIView alloc] init];
    lineView0.frame = CGRectMake(0.0f, 40.0f-0.5f, kDeviceWidth, 0.5f);
    lineView0.backgroundColor = kGreenColor;
    [_headerView addSubview:lineView0];
    
    
    UIView *lineView1 = [[UIView alloc] init];
    lineView1.frame = CGRectMake(kDeviceWidth/3.0f, 0.0f, 0.5f, 40.0f-0.5f);
    lineView1.backgroundColor = kGreenColor;
    [_headerView addSubview:lineView1];

    
    UIView *lineView2 = [[UIView alloc] init];
    lineView2.frame = CGRectMake(kDeviceWidth/3.0f*2.0f, 0.0f, 0.5f, 40.0f-0.5f);
    lineView2.backgroundColor = kGreenColor;
    [_headerView addSubview:lineView2];
    
    UIControl *lbl0Control = [[UIControl alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth/3.0f, 40.0f)];
    lbl0Control.tag = 116;
    [lbl0Control addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:lbl0Control];
    
    UIControl *lbl1Control = [[UIControl alloc] initWithFrame:CGRectMake(kDeviceWidth/3.0f, 0.0f, kDeviceWidth/3.0f, 40.0f)];
    lbl1Control.tag = 118;
    [lbl1Control addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:lbl1Control];

    UIControl *lbl2Control = [[UIControl alloc] initWithFrame:CGRectMake(kDeviceWidth/3.0f*2.0f, 0.0f, kDeviceWidth/3.0f, 40.0f)];
    lbl2Control.tag = 119;
    [lbl2Control addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:lbl2Control];

    //ic_down@2x.png
    lbl0 = [[UILabel alloc] init];
    lbl0.frame = CGRectMake(0.0, (40-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth/3.0f-26/2-2.0f, Home_Small_Size+2.0f);
    lbl0.font = kAppFont(Home_Small_Size+2.0f);
    lbl0.textColor = kBlackColor;
    lbl0.text = @"公司";
    lbl0.textAlignment = NSTextAlignmentLeft;
    [lbl0Control addSubview:lbl0];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_down"]];
    arrowImageView.frame = CGRectMake(kDeviceWidth/3.0f-26.0f/2.0f-2.0f, (40-11.0f/2.0f)/2.0f, 26/2, 11.0f/2.0f);
    [lbl0Control addSubview:arrowImageView];
    
    lbl1 = [[UILabel alloc] init];
    lbl1.frame = CGRectMake(0.0f, (40-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth/3.0f-26/2-2.0f, Home_Small_Size+2.0f);
    lbl1.font = kAppFont(Home_Small_Size+2.0f);
    lbl1.textColor = kBlackColor;
    lbl1.text = @"项目";
    lbl1.textAlignment = NSTextAlignmentLeft;
    [lbl1Control addSubview:lbl1];
    
    UIImageView *arrowImageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_down"]];
    arrowImageView1.frame = CGRectMake(kDeviceWidth/3.0f-26.0f/2.0f-2.0f, (40-11.0f/2.0f)/2.0f, 26/2, 11.0f/2.0f);
    [lbl1Control addSubview:arrowImageView1];

    
    lbl2 = [[UILabel alloc] init];
    lbl2.frame = CGRectMake(0.0f, (40-(Home_Small_Size+2.0f))/2.0f, kDeviceWidth/3.0f-26/2-2.0f, Home_Small_Size+2.0f);
    lbl2.font = kAppFont(Home_Small_Size+2.0f);
    lbl2.textColor = kBlackColor;
    lbl2.text = @"未开始";
    lbl2.textAlignment = NSTextAlignmentCenter;
    [lbl2Control addSubview:lbl2];
    
    UIImageView *arrowImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_down"]];
    arrowImageView2.frame = CGRectMake(kDeviceWidth/3.0f-26.0f/2.0f-2.0f, (40-11.0f/2.0f)/2.0f, 26/2, 11.0f/2.0f);
    [lbl2Control addSubview:arrowImageView2];
}

- (void)controlAction:(UIControl*)sender
{
    switch (sender.tag) {
        case 116:
        {
            SelectCompanyAndProjectViewController *selectVC = [[SelectCompanyAndProjectViewController alloc] init];
            selectVC.delegate = self;
            [self.navigationController pushViewController:selectVC animated:YES];
        }
            break;
        case 118:
        {
            SelectCompanyAndProjectViewController *selectVC = [[SelectCompanyAndProjectViewController alloc] init];
            selectVC.delegate = self;
            [self.navigationController pushViewController:selectVC animated:YES];
        }
            break;
        case 119:
        {
            ShiGongStatusViewController *shiGongVC = [[ShiGongStatusViewController alloc] init];
            shiGongVC.delegate = self;
            [self.navigationController pushViewController:shiGongVC animated:YES];
        }
            break;

        default:
            break;
    }
}

- (void)passData:(NSDictionary *)dic superDic:(NSDictionary *)superDic{
    secondDic = dic;
    firstDic = superDic;
    
    lbl0.text = [firstDic valueForKey:@"OrgName"];
    lbl1.text = [secondDic valueForKey:@"ProjectName"];
    
    [self getData];
}


- (void)passSelectStatus:(NSDictionary *)tempDic{
    if ([[tempDic valueForKey:@"Value"] intValue] == 0) {
        status = 0;
        lbl2.text = @"未开始";
    }else if ([[tempDic valueForKey:@"Value"] intValue] == 1){
        status = 1;
        lbl2.text = @"施工中";
    }else if ([[tempDic valueForKey:@"Value"] intValue] == 2){
        status = 2;
        lbl2.text = @"已竣工";
    }
    [self getData];
}


#pragma mark  ************************* UITableViewDataSource **************************************
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MultiSelectTableViewCell";
    CheckAndAcceptCell *cell = (CheckAndAcceptCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[CheckAndAcceptCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NSDictionary *dd = [_dataArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell showWithDataSearch:dd];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [_selFlags moveObjectFromIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];
}


#pragma mark -
#pragma mark Table view delegate

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dd = [_dataArray objectAtIndex:indexPath.row];
    if (tableView.isEditing)
    {
        MultiSelectTableViewCell *cell = (MultiSelectTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        [cell changeMSelectedState];
        //[_selFlags replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:cell.mSelected]];
    }
    else
    {
        GongXuDetailViewController *GXVC = [[GongXuDetailViewController alloc] initWithData:dd];
        GXVC.navTitle = @"详情";
        [self.navigationController pushViewController:GXVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
